package com.sakura.mall.coupon.couponSpuRelation.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.coupon.couponSpuRelation.domain.entity.CouponSpuRelationEntity;
import com.sakura.mall.coupon.couponSpuRelation.domain.request.AddCouponSpuRelationRequest;
import com.sakura.mall.coupon.couponSpuRelation.domain.request.UpdateCouponSpuRelationRequest;
import com.sakura.mall.coupon.couponSpuRelation.domain.request.CouponSpuRelationPageRequest;
import com.sakura.mall.coupon.couponSpuRelation.domain.vo.CouponSpuRelationVo;
import com.sakura.mall.coupon.couponSpuRelation.mapper.CouponSpuRelationMapper;
import com.sakura.mall.coupon.couponSpuRelation.service.CouponSpuRelationService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 优惠券与产品关联 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class CouponSpuRelationServiceImpl extends ServiceImpl<CouponSpuRelationMapper, CouponSpuRelationEntity> implements CouponSpuRelationService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddCouponSpuRelationRequest request) {
        // todo 其他校验逻辑
        CouponSpuRelationEntity couponSpuRelation = request.convert(CouponSpuRelationEntity.class);
        this.save(couponSpuRelation);
    }

    @Override
    public Page<CouponSpuRelationVo> queryPage(CouponSpuRelationPageRequest request) {
        IPage<CouponSpuRelationEntity> qp = request.toPage();
        LambdaQueryWrapper<CouponSpuRelationEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<CouponSpuRelationVo> ret = qp.convert(entity -> entity.convert(CouponSpuRelationVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateCouponSpuRelationRequest request) {
        CouponSpuRelationEntity entity = request.convert(CouponSpuRelationEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

